//
//  DrawView.m
//  画图
//
//  Created by Justsoso on 16/4/7.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "DrawView.h"

#import "DrawLinePath.h"

@interface DrawView()

/* 当前绘制的路径 */
@property (nonatomic,strong) DrawLinePath *curPath;

/* 路径数组 */
@property (nonatomic,strong) NSMutableArray *pathArr;

@end

@implementation DrawView

- (void)awakeFromNib
{
    self.curLineWidth = 1;
    self.curColor = [UIColor blackColor];
}

- (NSMutableArray *)pathArr
{
    if (_pathArr == nil)
    {
        _pathArr = [NSMutableArray array];
    }
    return _pathArr;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    for (DrawLinePath *path in self.pathArr)
    {
        [path.color setStroke];
        [path stroke];
    }
}

- (void)undo
{
    [self.pathArr removeLastObject];
    
    [self setNeedsDisplay];
}
- (void)clear
{
    [self.pathArr removeAllObjects];
    
    [self setNeedsDisplay];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    /* 创建一个新路径 */
    self.curPath = [DrawLinePath bezierPath];
    self.curPath.lineWidth = self.curLineWidth;
    self.curPath.color = self.curColor;
    /* 将路径添加到路径数组中 */
    [self.pathArr addObject:self.curPath];
    /* 确定一个起点 */
    [self.curPath moveToPoint:point];
    
    [self setNeedsDisplay];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    /* 得到移动到位置 */
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    /* 点 添加到路径中 */
    [self.curPath addLineToPoint:point];
    
    [self setNeedsDisplay];
}
@end
